package org.dant.ant.extension.tasks.internal;

import java.util.*;
import java.io.*;
import java.lang.reflect.*;

import org.dant.ant.extension.utils.ParameterUtil;
import org.dant.ant.extension.utils.StringUtils;

public class RemoteCommand implements Serializable, Cloneable {

  protected String name;
  protected String sourceHost;
  protected Map parameters;

  public RemoteCommand() {
    parameters = new HashMap();
  }
  
  public String getName() {
    return this.name;
  }

  public String getSourceHost() {
    return this.sourceHost;
  }
  
  public Map getParameters() {
    return this.parameters;
  }

  public void setParameters(Map map) {
    this.parameters = map;
  }

  public void setSourceHost(String sh) {
    this.sourceHost = sh;
  }

  public void setName(String name) {
    this.name = name;
  }

  public void appendParameters(Map param) {
    this.parameters.putAll(param);
  }

  public void appendParameters(Object key, Object value) {
    this.parameters.put(key, value);
  }

  public Object getParameter(String key) {
    return this.parameters.get(key);
  }

  public void init() throws Exception {
    appendParameters(ParameterUtil.resolve(this, true));
  }

  public Object clone() throws CloneNotSupportedException {
    RemoteCommand rcmd = (RemoteCommand) super.clone();
    rcmd.setParameters((Map)((HashMap)parameters).clone());
    return rcmd;
  }

  public String toString() {
    StringBuffer sb = new StringBuffer();
    sb.append("\n------------------------------------"); 
    sb.append("\nCommand Name: ");
    sb.append(this.getName());
    sb.append("\nSource Host: ");
    sb.append(this.sourceHost);
    sb.append("\nParameters: ");
    sb.append("\nKey\t Value \t Type");
    sb.append("\n---------               ----------\n"); 
    for (Iterator iter=parameters.keySet().iterator(); iter.hasNext();) {
      String key = (String) iter.next();
      sb.append(key);
      Object obj = parameters.get(key);
      if (obj != null && obj.getClass().isArray()) {
        for (int i=0; i<Array.getLength(obj); i++) {
          Object a = Array.get(obj, i);
          if (a != null) {
            sb.append("\t");
            sb.append(a);
            sb.append("\t");
            sb.append(a.getClass());
            sb.append("\n");
            sb.append(StringUtils.duplicate(" ", key.length()));
          }
        }
      } else if (obj instanceof List) {
        List li = (List) obj;
        for (int i=0; i<li.size(); i++) {
          Object a = li.get(i);
          if (a != null) {
            sb.append("\t");
            sb.append(a);
            sb.append("\t");
            sb.append(a.getClass());
            sb.append(StringUtils.duplicate(" ", key.length()));
          }
        }
      } else {
        sb.append("\t");
        sb.append(obj);
        sb.append("\t");
        if (obj != null) sb.append(obj.getClass());
      }
      sb.append("\n");
    }
    sb.append("------------------------------------\n");
    return sb.toString();
  }
}
